#include "DXUT.h"
#include "MathUtil.h"

/**
 * Clamps a given value to be an element of [0..1].
 */
double clamp(double value) {
	if (value > 1.0)
		return 1.0;
	if (value < 0.0)
		return 0.0;
	return value;
};

double clamp(double value, double min, double max) {
	if (value > max)
		return max;
	if (value < min)
		return min;
	return value;
};

float clamp(float value, float min, float max) {
	if (value > max)
		return max;
	if (value < min)
		return min;
	return value;
};

int clamp(int value, int min, int max) {
	if (value > max)
		return max;
	if (value < min)
		return min;
	return value;
};

/**
 * Linear interpolation.
 */
double lerp(double x, double x1, double x2, double q00, double q01) {
	return ((x2 - x) / (x2 - x1)) * q00 + ((x - x1) / (x2 - x1)) * q01;
};

double lerp(double x1, double x2, double p) {
	return x1 * (1.0 - p) + x2 * p;
};

float lerpf(float x1, float x2, float p) {
	return x1 * (1.0f - p) + x2 * p;
};

/**
 * Trilinear interpolation.
 */
double triLerp(double x, double y, double z, double q000, double q001, double q010, double q011, double q100, double q101, double q110, double q111, double x1, double x2, double y1, double y2, double z1, double z2) {
        double x00 = lerp(x, x1, x2, q000, q100);
        double x10 = lerp(x, x1, x2, q010, q110);
        double x01 = lerp(x, x1, x2, q001, q101);
        double x11 = lerp(x, x1, x2, q011, q111);
        double r0 = lerp(y, y1, y2, x00, x01);
        double r1 = lerp(y, y1, y2, x10, x11);
        return lerp(z, z1, z2, r0, r1);
};